<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="签约商户" v-bind="validateInfos.businessId">
            <j-search-select placeholder="请选择签约商户" v-model:value="formData.businessId" dict="caishui_business,name,id" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="服务公司" v-bind="validateInfos.companyId">
	          <j-search-select placeholder="请选择服务公司" v-model:value="formData.companyId" dict="caishui_service_company,name,id" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="签约服务类型" v-bind="validateInfos.signingService" :labelCol="labelColItem" :wrapperCol="wrapperColItem">
	          <j-select-multiple type="list_multi" v-model:value="formData.signingService" dictCode="caishui_signing_service,name,id" placeholder="请选择签约服务类型" :disabled="disabled" :triggerChange="false"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="任务介绍" v-bind="validateInfos.introduce" :labelCol="labelColItem" :wrapperCol="wrapperColItem">
            <a-textarea type="textarea" :rows="3" v-model:value="formData.introduce" placeholder="请输入任务介绍" :disabled="disabled"></a-textarea>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="状态" v-bind="validateInfos.status">
            <j-dict-select-tag v-model:value="formData.status" dictCode="status" stringToNumber="true" placeholder="请选择状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
<!--        <a-col :span="12">
          <a-form-item label="审核状态" v-bind="validateInfos.checkStatus">
            <j-dict-select-tag v-model:value="formData.checkStatus" dictCode="check_status" stringToNumber="true" placeholder="请选择审核状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>-->
        <a-col :span="12">
          <a-form-item label="合作邀请码" v-bind="validateInfos.invitationCodeWx">
            <j-image-upload v-if="!disabled" :fileMax=1 bizPath="business/serviceTask"
                            v-model:value="formData.invitationCodeWx"></j-image-upload>
            <Image v-else :width="240" :src="formData.invitationCodeWxImg"
                   fallback=""
            />
          </a-form-item>
        </a-col>
<!--        <a-col :span="12">
          <a-form-item label="支付宝邀请码" v-bind="validateInfos.invitationCodeZfb">
            <j-image-upload v-if="!disabled" :fileMax=1 bizPath="business/serviceTask"
                            v-model:value="formData.invitationCodeZfb"></j-image-upload>
            <Image v-else :width="240" :src="formData.invitationCodeZfbImg"
                   fallback=""
            />
          </a-form-item>
        </a-col>-->
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSwitch from '/@/components/Form/src/jeecg/components/JSwitch.vue';
  import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../ServiceTask.api';
  import { Form, Image } from 'ant-design-vue';
  import { useGlobSetting } from "/@/hooks/setting";
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    companyId: '',   
    businessId: '',   
    signingService: '',   
    title: '',   
    introduce: '',   
    invitationCodeWx: '',   
    invitationCodeZfb: '',
    status: 1,
    checkStatus: 1,
    invitationCodeWxImg: '',
    invitationCodeZfbImg: '',
  });
  const { createMessage } = useMessage();
  const { filePreviewUrl } = useGlobSetting();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 6 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const labelColItem = ref<any>({ xs: { span: 24 }, sm: { span: 3 } });
  const wrapperColItem = ref<any>({ xs: { span: 24 }, sm: { span: 20 } });
  const confirmLoading = ref<boolean>(false);

  //表单验证
  const validatorRules = {
    companyId: [{ required: true, message: '请选择服务公司!'},],
    businessId: [{ required: true, message: '请选择签约商户!'},],
    signingService: [{ required: true, message: '请选择签约服务类型!'},],
    title: [{ required: true, message: '请输入任务标题!'},],
    invitationCodeWx: [{ required: true, message: '请上传微信合作邀请码!'},],
    // invitationCodeZfb: [{ required: true, message: '请上传支付宝合作邀请码!'},],
    status: [],
    checkStatus: [],
  };
  const { resetFields, validate, validateInfos, clearValidate } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    let bool = false;
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        bool = false;
      }else{
        bool =  true;
      }
    }
    bool = props.formDisabled;
    if(bool){
      //清除表单验证
      clearValidate();
    }
    return bool;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    record.invitationCodeWxImg = filePreviewUrl + record.invitationCodeWx;
    // record.invitationCodeZfbImg = filePreviewUrl + record.invitationCodeZfb;
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //数据格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
